package com.wzy.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.wzy.dao.BaseDao;
import com.wzy.dao.PetOwnerDao;
import com.wzy.entity.PetOwner;

public class PetOwnerDaoImpl extends BaseDao implements PetOwnerDao{

	@Override
	public List<PetOwner> getAllPetOwner() {
		List<PetOwner> list = new ArrayList<PetOwner>();
		
		Connection conn = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		
		try {
			conn = super.getConnection();
			String sql = "select * from petowner";
			pstmt = conn.prepareStatement(sql);
			
			rs = pstmt.executeQuery();
			
			while (rs.next()) {
				PetOwner po = new PetOwner();
				
				po.setId(rs.getInt("id"));
				po.setMoney(rs.getDouble("money"));
				po.setName(rs.getString("name"));
				po.setPassword(rs.getString("password"));
				
				list.add(po);
			}
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			super.closeAll(conn, pstmt, rs);
		}
		
		
		return list;
	}

	@Override
	public PetOwner getPetOwner(String name, String pwd) {
		PetOwner po = null;
		
		Connection conn = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		
		try {
			conn = super.getConnection();
			String sql = "select * from petowner where name=? and password=?";
			pstmt = conn.prepareStatement(sql);
			
			pstmt.setString(1, name);
			pstmt.setString(2, pwd);
			
			rs = pstmt.executeQuery();
			
			if (rs.next()) {
				po = new PetOwner();
				po.setId(rs.getInt("id"));
				po.setMoney(rs.getDouble("money"));
				po.setName(rs.getString("name"));
				po.setPassword(rs.getString("password"));
				
			}
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			super.closeAll(conn, pstmt, rs);
		}
		
		return po;
	}

}
